package com.czmh.etc.onlinepubsys.controller.wj;

import com.alibaba.fastjson.JSON;
import com.czmh.etc.onlinepubsys.configuration.WxAppletConfig;
import com.czmh.etc.onlinepubsys.constants.wj.BusinessType;
import com.czmh.etc.onlinepubsys.entity.wj.CurrentContext;
import com.czmh.etc.onlinepubsys.json.mingchuang.JSONResult;
import com.czmh.etc.onlinepubsys.request.wj.checkSignedRequest;
import com.czmh.etc.onlinepubsys.response.ue.ApiResponse;
import com.czmh.etc.onlinepubsys.services.EtcPayOrderService;
import com.czmh.etc.onlinepubsys.services.EtcUserInfoSevice;
import com.czmh.etc.onlinepubsys.services.wj.WjIEtcOrderService;
import com.czmh.etc.onlinepubsys.services.wj.WjICurrentContextService;
import com.czmh.etc.onlinepubsys.utils.RedisUtil;
import com.czmh.etc.onlinepubsys.utils.wj.WjHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/{version}/api/wj/order")
@Api(tags = "ETC设备激活接口相关【新】- 万集")
public class WjActiveController {

    @Autowired
    private WxAppletConfig config;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private WjICurrentContextService currentContextService;

    @Autowired
    private EtcPayOrderService etcPayOrderService;

    @Autowired
    private EtcUserInfoSevice etcUserInfoSevice;

    @Autowired
    private WjIEtcOrderService wjEtcOrderService;

    @PostMapping("/checkSigned")
    @ApiOperation("查询是否可签约接口 *激活")
    public JSONResult checkSigned(@RequestBody checkSignedRequest req) {
        log.info("查询是否可签约接口:{}", JSON.toJSONString(req));
        JSONResult result = new JSONResult();
        CurrentContext ctx = currentContextService.getByUserId(req.getUserId());
        if (ctx == null) {
            result.setCode(JSONResult.FAIL);
            result.setMessage("暂无订单，请稍后再试！");
            return result;
        }
        ApiResponse r = WjHttpUtil.get(BusinessType.CHECK_SIGNED, JSON.toJSONString(req));
        if (r != null && r.getSuccess()) {
            if (r.getSuccess()) {
                result.setRspData(r.getRspData());
                result.setMessage(r.getMsg());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r != null ? r.getMessage() : "服务器开小差，请稍后再试！");
        }
        return result;
    }

    @PostMapping("/bluetoothSign")
    @ApiOperation("蓝牙标签签约接口 *激活")
    public JSONResult bluetoothSign(@RequestBody checkSignedRequest req) {
        log.info("蓝牙标签签约接口:{}", JSON.toJSONString(req));
        JSONResult result = new JSONResult();
        CurrentContext ctx = currentContextService.getByUserId(req.getUserId());
        if (ctx == null) {
            result.setCode(JSONResult.FAIL);
            result.setMessage("暂无订单，请稍后再试！");
            return result;
        }
        ApiResponse r = WjHttpUtil.get(BusinessType.BLUE_TOOTH_SIGN, JSON.toJSONString(req));
        if (r != null && r.getSuccess()) {
            if (r.getSuccess()) {
                result.setRspData(r.getRspData());
                result.setMessage(r.getMsg());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r != null ? r.getMessage() : "服务器开小差，请稍后再试！");
        }
        return result;
    }

    @PostMapping("/startOrderOnlineST")
    @ApiOperation("订单开始发行及校验接口 *激活")
    public JSONResult startOrderOnlineST(@RequestBody checkSignedRequest req) {
        log.info("订单开始发行及校验接口:{}", JSON.toJSONString(req));
        JSONResult result = new JSONResult();
        CurrentContext ctx = currentContextService.getByUserId(req.getUserId());
        if (ctx == null) {
            result.setCode(JSONResult.FAIL);
            result.setMessage("暂无订单，请稍后再试！");
            return result;
        }
        ApiResponse r = WjHttpUtil.get(BusinessType.START_ORDER_ONLINE_ST, JSON.toJSONString(req));
        if (r != null && r.getSuccess()) {
            if (r.getSuccess()) {
                result.setRspData(r.getRspData());
                result.setMessage(r.getMsg());
            }
        } else {
            result.setCode(r != null ? r.getCode() : JSONResult.FAIL);
            result.setMessage(r != null ? r.getMessage() : "服务器开小差，请稍后再试！");
        }
        return result;
    }
}
